# +----------------------------------------------------------------------
# | ChatWork智能聊天办公系统
# +----------------------------------------------------------------------
# | 软件声明: 本系统并非自由软件,未经授权任何形式的商业使用均属非法。
# | 版权保护: 任何企业和个人不允许对程序代码以任何形式任何目的复制/分发。
# | 授权要求: 如有商业使用需求,请务必先与版权所有者取得联系并获得正式授权。
# +----------------------------------------------------------------------
# | Author: ChatWork Team <2474369941@qq.com>
# +----------------------------------------------------------------------
from typing import Union, List, Dict
from decimal import Decimal
from fastapi import Query
from pydantic import BaseModel, Field


class PricingVo(BaseModel):
    id: int = Field(default=0, ge=0, description="ID")
    tags: str = Field(default="", description="价格标签")
    calendar: str = Field(..., pattern=r"^(day|month|year|perpetual)$", description="时长类型")
    duration: int = Field(..., ge=0, le=99999999, description="套餐时长")
    give_points: int = Field(..., ge=0, le=999999999, description="赠送积分")
    sell_price: Decimal = Field(..., max_digits=10, decimal_places=2, description="销售价格")
    line_price: Decimal = Field(..., max_digits=10, decimal_places=2, description="划线价格")
    sort: int = Field(default=0, ge=0, le=999999999, description="排序编号")
    is_builtin: int = Field(..., ge=0, le=1, description="系统内置: [0=否, 1=是]")
    is_disable: int = Field(..., ge=0, le=1, description="是否禁用: [0=否, 1=是]")


class PrivilegeVo(BaseModel):
    id: int = Field(default=0, ge=0, description="ID")
    name: str = Field(..., max_length=100, description="应用名称")
    logo: str = Field(..., max_length=300, description="应用图标")
    scene: str = Field(..., max_length=30, description="套餐场景: [chat,vector,music,video]")
    marks: str = Field(..., max_length=100, description="渠道标记: [模型标记/应用标记]")
    daily_limit: int = Field(..., ge=0, le=99999999, description="每日免费次数")
    is_disable: int = Field(..., ge=0, le=1, description="是否禁用: [0=否, 1=是]")


class SpecificsVo(BaseModel):
    id: int = Field(default=0, ge=0, description="ID")
    name: str = Field(..., max_length=200, description="权益名称")
    description: str = Field(default="", max_length=200, description="权益描述")
    sort: int = Field(default=0, ge=0, le=999999999, description="排序编号")
    is_support: int = Field(..., ge=0, le=1, description="是否支持: [0=否, 1=是]")
    is_disable: int = Field(..., ge=0, le=1, description="是否禁用: [0=否, 1=是]")


"""--------------- Separator ---------------"""


class SubscribePricingIn(BaseModel):
    """ 套餐定价搜索参数 """
    user_id: int = Field(..., ge=0, description="用户ID")
    calendar: Union[str, None] = Query(default="", description="日期类型")

    class Config:
        json_schema_extra = {
            "example": {
                "user_id": 1,
                "calendar": "month"
            }
        }


class SubscribeDetailIn(BaseModel):
    """ 订阅套餐详情参数 """
    id: int = Query(..., gt=0, description="套餐ID")


class SubscribeAddIn(BaseModel):
    """ 订阅套餐新增参数 """
    name: str = Field(..., min_length=1, max_length=100, description="套餐名称")
    description: str = Field(default="", max_length=500, description="套餐描述")
    train_num: Union[int, None] = Field(default=0, ge=0, le=999999999, description="训练组数量")
    robot_num: Union[int, None] = Field(default=0, ge=0, le=999999999, description="智能体数量")
    know_num: Union[int, None] = Field(default=0, ge=0, le=999999999, description="知识库数量")
    mcp_num: Union[int, None] = Field(default=0, ge=0, le=999999999, description="MCP数量")
    sort: int = Field(default=0, ge=0, le=999999999, description="排序编号")
    is_disable: int = Field(default=0, ge=0, le=1, description="是否禁用: [0=否, 1=是]")
    pricing_list: List[PricingVo] = Field(default=[], description="是否禁用: [0=否, 1=是]")
    specific_list: List[SpecificsVo] = Field(default=[], description="是否禁用: [0=否, 1=是]")
    apply_list: List[PrivilegeVo] = Field(default=[], description="应用权益")

    @classmethod
    def messages(cls):
        return {
            "name.min_length": "请填写套餐名称",
            "name.max_length": "套餐名称不能超出100个字符",
            "tags.max_length": "套餐标签不能超出100个字符",
            "sell_price.missing": "请填写销售价格",
            "sell_price.max_digits": "销售价格整数部分不能超出8位",
            "sell_price.decimal_places": "销售价格只能保留2位小数",
            "line_price.missing": "请填写划线价格",
            "line_price.max_digits": "划线价格整数部分不能超出8位",
            "line_price.decimal_places": "划线价格只能保留2位小数",
            "sort.ge": "排序号不能少于0",
            "is_disable.ge": "状态非合法值: [0, 1]",
            "is_disable.le": "状态非合法值: [0, 1]"
        }

    class Config:
        json_schema_extra = {
            "example": {
                "name": "尊享套餐",
                "tags": "限时特惠",
                "sell_price": 10.88,
                "line_price": 20.98,
                "points_num": 100,
                "train_num": 20,
                "robot_num": 20,
                "know_num": 20,
                "mcp_num": 20,
                "sort": 10,
                "is_disable": 0
            }
        }


class SubscribeEditIn(BaseModel):
    """ 订阅套餐编辑参数 """
    id: int = Field(..., gt=0, description="ID")
    name: str = Field(..., min_length=1, max_length=100, description="套餐名称")
    description: str = Field(max_length=500, description="套餐描述")
    train_num: Union[int, None] = Field(default=0, ge=0, le=999999999, description="训练组数量")
    robot_num: Union[int, None] = Field(default=0, ge=0, le=999999999, description="智能体数量")
    know_num: Union[int, None] = Field(default=0, ge=0, le=999999999, description="知识库数量")
    mcp_num: Union[int, None] = Field(default=0, ge=0, le=999999999, description="MCP数量")
    sort: int = Field(default=0, ge=0, le=999999999, description="排序编号")
    is_disable: int = Field(default=0, ge=0, le=1, description="是否禁用: [0=否, 1=是]")
    pricing_list: List[PricingVo] = Field(default=[], description="是否禁用: [0=否, 1=是]")
    specific_list: List[SpecificsVo] = Field(default=[], description="是否禁用: [0=否, 1=是]")
    apply_list: List[PrivilegeVo] = Field(default=[], description="应用权益")

    @classmethod
    def messages(cls):
        return {
            "name.min_length": "请填写套餐名称",
            "name.max_length": "套餐名称不能超出100个字符",
            "tags.max_length": "套餐标签不能超出100个字符",
            "sell_price.missing": "请填写销售价格",
            "sell_price.max_digits": "销售价格整数部分不能超出8位",
            "sell_price.decimal_places": "销售价格只能保留2位小数",
            "line_price.missing": "请填写划线价格",
            "line_price.max_digits": "划线价格整数部分不能超出8位",
            "line_price.decimal_places": "划线价格只能保留2位小数",
            "sort.ge": "排序号不能少于0",
            "is_disable.ge": "状态非合法值: [0, 1]",
            "is_disable.le": "状态非合法值: [0, 1]"
        }

    class Config:
        json_schema_extra = {
            "example": {
                "name": "尊享套餐",
                "tags": "限时特惠",
                "sell_price": 10.88,
                "line_price": 20.98,
                "points_num": 100,
                "train_num": 20,
                "robot_num": 20,
                "know_num": 20,
                "mcp_num": 20,
                "sort": 10,
                "is_disable": 0
            }
        }


class SubscribeDeleteIn(BaseModel):
    """ 订阅套餐删除参数 """
    id: int = Field(..., gt=0, description="ID")

    class Config:
        json_schema_extra = {
            "example": {
                "id": 1
            }
        }


class SubscribeConfigIn(BaseModel):
    """ 订阅套餐配置参数 """
    status: int = Field(..., ge=0, le=1, description="功能状态", examples=[1])
    timeline: Dict[str, bool] = Field(..., description="时间选项")

    class Config:
        json_schema_extra = {
            "example": {
                "status": 1,
                "timeline": {
                    "day": False,
                    "month": True,
                    "year": True,
                    "perpetual": False
                }
            }
        }


class SubscribeOpenIn(BaseModel):
    """ 订阅套餐开通参数 """
    user_id: int = Field(..., ge=0, le=1, description="用户ID", examples=[1])
    pricing_id: int = Field(..., ge=0, description="套餐ID", examples=[1])
    paid_amount: Decimal = Field(max_digits=10, decimal_places=2, description="应付金额")

    class Config:
        json_schema_extra = {
            "example": {
                "user_id": 1,
                "pricing_id": 1,
                "paid_amount": 99.99
            }
        }


"""--------------- Separator ---------------"""


class SubscribePricingVo(BaseModel):
    """ 订阅售价列表Vo """
    id: int = Field(description="套餐ID")
    pricing_id: int = Field(description="定价ID")
    name: str = Field(description="套餐名称")
    tags: str = Field(description="套餐标签")
    description: str = Field(description="套餐描述")
    sell_price: Decimal = Field(description="销售价格")
    line_price: Decimal = Field(description="划线价格")
    my_version: int = Field(description="我的版本: [0=否, 1=是]")
    is_purchase: int = Field(description="允许购买: [0=否, 1=是]")
    is_builtin: int = Field(description="系统内置: [0=否, 1=是]")
    is_disable: int = Field(description="是否禁用: [0=否, 1=是]")

    class Config:
        json_schema_extra = {
            "example": {
                "id": 1,
                "pricing_id": 1,
                "name": "0.01",
                "tags": "tags",
                "description": "this is description",
                "sell_price": 10.88,
                "line_price": 168.80,
                "my_version": 0,
                "is_purchase": 1,
                "is_builtin": 1,
                "is_disable": 0
            }
        }


class SubscribePackageListVo(BaseModel):
    """ 订阅套餐列表Vo """
    id: int = Field(description="套餐ID")
    name: str = Field(description="套餐名称")
    description: str = Field(description="套餐描述")
    train_num: int = Field(description="训练条数")
    robot_num: int = Field(description="智能体数")
    know_num: int = Field(description="知识库数")
    mcp_num: int = Field(description="MCP数")
    min_price: Decimal = Field(description="最低售价")
    max_price: Decimal = Field(description="最高售价")
    sort: int = Field(description="排序编号")
    is_builtin: int = Field(description="系统内置: [0=否, 1=是]")
    is_disable: int = Field(description="是否禁用: [0=否, 1=是]")
    create_time: str = Field(description="创建时间")
    update_time: str = Field(description="更新时间")

    class Config:
        json_schema_extra = {
            "example": {
                "id": 1,
                "name": "基础版",
                "description": "先人一步体验 AI 生产力",
                "train_num": 10,
                "robot_num": 10,
                "know_num": 10,
                "mcp_num": 10,
                "min_price": 9.99,
                "max_price": 16.89,
                "sort": 10,
                "is_builtin": 0,
                "is_disable": 0,
                "create_time": "2023-07-22 11:33:44",
                "update_time": "2023-07-22 11:33:44"
            }
        }


class SubscribeDetailVo(BaseModel):
    id: int = Field(description="套餐ID")
    name: str = Field(description="套餐名称")
    description: str = Field(description="套餐描述")
    robot_num: int = Field(description="智能体数")
    know_num: int = Field(description="知识库数")
    train_num: int = Field(description="训练组数")
    mcp_num: int = Field(description="MCP数")
    sort: int = Field(description="排序编号")
    is_builtin: int = Field(description="系统内置: [0=否, 1=是]")
    is_disable: int = Field(description="是否禁用: [0=否, 1=是]")
    pricing_list: List[PricingVo] = Field(description="定价列表")
    specific_list: List[SpecificsVo] = Field(description="权益列表")
    apply_list: List[PrivilegeVo] = Field(description="应用权益")
